package ebj.common.math;

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Random;

public class MathUtils {
	public static String numberFormat(Number number, int dcmlDigits) {
		NumberFormat numberFormat = NumberFormat.getInstance();
		numberFormat.setMaximumFractionDigits(dcmlDigits);
		return numberFormat.format(number);
	}
	
	public static String numberFormat2(Number number, int dcmlDigits) {
		StringBuilder stringBuilder = new StringBuilder("#");
		if (dcmlDigits > 0) {
			stringBuilder.append(".");
			for (int i = 0; i < dcmlDigits; i++) {
				stringBuilder.append("0");
			}
		}
		DecimalFormat decimalFormat = new DecimalFormat(stringBuilder.toString());
		return decimalFormat.format(number);
	}
	
	/**
	 * 生成[0,n]的随机数
	 * @return
	 */
	public static int random(int n) {
		if (n > 0){
			return new Random().nextInt(n) + 1;
		}else {
			return 0;
		}
	}
	
	/**
	 * 生成[m,n]范围内的随机数
	 * @param m
	 * @param n
	 * @return
	 */
	public static int betweenRandom(int m, int n) {
		if (m > 0 && n > m) {
			return (new Random().nextInt(n-m+1)) + m;
		}else {
			return 0;
		}
	}
}
